package tournament;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;

import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.mxEdgeLabelLayout;
import com.mxgraph.layout.mxFastOrganicLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.layout.mxParallelEdgeLayout;
import com.mxgraph.layout.mxPartitionLayout;
import com.mxgraph.layout.mxStackLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.layout.orthogonal.mxOrthogonalLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.util.mxMorphing;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;
 

public class TournamentLayoutPanel extends JPanel {

	private static final long serialVersionUID = 196831535599934813L;
	public mxCell cells[][];
	public final mxGraph graph;
	public Object defaultParent;
	public mxGraphComponent graphComponent;
	public Tournament tournament;
	public TournamentLayoutPanel(Tournament tournament) {
		//super("TournamentFrame");
		
		this.graph = new mxGraph();
		this.defaultParent = this.graph.getDefaultParent();
		this.graphComponent = new mxGraphComponent(this.graph);
		this.tournament = tournament;
		createNodes(tournament);
		this.setBorder(null);
		

	}
	
	public void createNodes(Tournament tournament){
		graph.setAutoOrigin(true);
		graph.setAutoSizeCells(true);

		//this.getContentPane().add( graphComponent);

		graph.getModel().beginUpdate();  
		int bracketSize = getBracketAmount(tournament.maxCompetitors, 2);

		int nodeCount = tournament.maxCompetitors;
		cells = new mxCell[bracketSize+2][tournament.maxCompetitors]; // bracket, pos 
		for(int k = 0; k <= bracketSize; k++){
			
			if(k > 0){
				nodeCount = nodeCount / 2;
			}

			//System.out.println(nodeCount);
			for(int i = 0; i <= nodeCount-1; i++){
				if(i < nodeCount){
					createCell(k, i);
					//System.out.println(k + " " + i);
				}
			}	

		}
		
		//createCell(bracketSize, 0);
		nodeCount = tournament.maxCompetitors;

		for(int b = 0; b < bracketSize; b++){
				
			
			int target = 0;
			int counter = 0;
			//System.out.println(nodeCount);
			for(int i = 0; i <= nodeCount-1; i++){
				if(b < bracketSize){
					graph.insertEdge(graph.getDefaultParent(), null, "", cells[b][i], cells[b+1][target]);
					//System.out.println("child: " + cells[b][i].getValue() + " parent: " + cells[b+1][target].getValue());
				}
		
				counter++;
				if(counter == 2){
					target++;
					counter = 0;
				}
				
				
			}
			nodeCount = nodeCount / 2;
		}
		
		graph.insertEdge(graph.getDefaultParent(), null, "", cells[bracketSize-1][0], cells[bracketSize+1][0]);
		graph.insertEdge(graph.getDefaultParent(), null, "", cells[bracketSize-1][1], cells[bracketSize+1][0]);
	
		
		//mxCompactTreeLayout
		mxCompactTreeLayout layout = new mxCompactTreeLayout(graph);
		layout.setEdgeRouting(false);
		layout.setHorizontal(false);
		layout.setInvert(true);
		layout.setLevelDistance(10);
		layout.setNodeDistance(5);
		//mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
		//layout.setOrientation(7);

		layout.execute(graph.getDefaultParent());

		graph.getModel().endUpdate();
		
		graphComponent.zoomTo(0.9, false);
		graphComponent.setBorder(null);
		this.add( graphComponent);
		
	}
	
	public void setCellText(int bracket, int pos, String text){
		try{
			graph.getModel().setValue(cells[bracket][pos], text);
		}catch(Exception e){
			System.out.println("Error - setcellText");
		}
		
	}
	
	public void createCell(int bracket, int pos){
		mxCell cell = new mxCell();
		
		cell.setAttribute("Position", String.valueOf(pos));
		cell.setAttribute("Bracket",  String.valueOf(bracket));
		mxGeometry geom = new mxGeometry();
		geom.add(new mxRectangle(20,20, 80,10));
		cell.setGeometry(geom);
		cell.setVertex(true);
		cell.setVisible(true);
		cell.setValue("Pos: " + pos + " Bracket: " + bracket);
		graph.addCell(cell, defaultParent);
		//System.out.println(bracket);
		cells[bracket][pos] = cell;
	}
	
	public mxCell getCellByBracketPos(int bracket, int pos){
		try{
			mxCell cell = cells[bracket][pos];
			return cell;
		}catch(Exception e){
			System.out.println("error getCellByPosBracket");
		}
		return null;
	}
	
	
	public int getBracketAmount(int x,int y)
	{
	    int e=0;
	    while((x%y)==0)
	    {
	        x/=y;
	        e++;
	    }
	    return e;
	}



//	public static void main(String args[]) {
//		TournamentFrame frame = new TournamentFrame();
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		frame.setSize(400, 320);
//		frame.setVisible(true);
//	}
}
